package com.pxst.merchant.service.impl;



import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pxst.merchant.entity.SysMerchantWhiteListEntity;
import com.pxst.merchant.mapper.SysMerchantWhiteListMapper;
import com.pxst.merchant.req.WhiteListReq;
import com.pxst.merchant.resp.WhiteListResp;
import com.pxst.merchant.service.SysMerchantWhiteListService;
import com.pxst.resp.ApiResp;
import com.pxst.resp.PageResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 28167
* @description 针对表【sys_merchant_white_list(商户白名单（sh130）)】的数据库操作Service实现
* @createDate 2023-09-14 17:20:07
*/
@Slf4j
@Service
public class SysMerchantWhiteListServiceImpl extends ServiceImpl<SysMerchantWhiteListMapper, SysMerchantWhiteListEntity> implements SysMerchantWhiteListService {

    @Autowired
    private SysMerchantWhiteListMapper sysMerchantWhiteListMapper;

    @Override
    public ApiResp<PageResp<WhiteListResp>> getWhiteListByConditions(WhiteListReq conditions) {
        if (conditions.getStartDate() != null && conditions.getEndDate()!= null){
            if (conditions.getStartDate().compareTo(conditions.getEndDate()) > 0){
                return ApiResp.paramError("时间段开始晚于时间段结束");
            }
        }
        PageHelper.startPage(conditions.getPage(),conditions.getRp());
        Page<WhiteListResp> page = (Page<WhiteListResp>) sysMerchantWhiteListMapper.getMerchantWhiteListByConditions(conditions);
        return ApiResp.page(page);
    }

    @Override
    public ApiResp<SysMerchantWhiteListEntity> addWhiteListMember(SysMerchantWhiteListEntity entity) {

        // 商户id和ip不能同时相同
        QueryWrapper<SysMerchantWhiteListEntity> wrapper = new QueryWrapper<>();
        wrapper
                .eq("merchant_id",entity.getMerchantId())
                .eq("ip",entity.getIp());
        SysMerchantWhiteListEntity member = sysMerchantWhiteListMapper.selectOne(wrapper);
        if (member != null){
            return ApiResp.paramError("白名单中已存在该条记录");
        }
        sysMerchantWhiteListMapper.insert(entity);
        return ApiResp.sucess(entity);
    }

    @Override
    public ApiResp updateWhiteListMember(SysMerchantWhiteListEntity entity) {
        if (entity.getId() == null){
            return ApiResp.paramError("白名单记录id不能为空");
        }
        SysMerchantWhiteListEntity existEntity = sysMerchantWhiteListMapper.selectById(entity.getId());
        if (existEntity == null){
            return ApiResp.paramError("该条记录不存在");
        }
        // 判断修改的记录是否存在
        QueryWrapper<SysMerchantWhiteListEntity> wrapper = new QueryWrapper<>();
        wrapper
                .eq("merchant_id",entity.getMerchantId())
                .eq("ip",entity.getIp());
        SysMerchantWhiteListEntity selected = sysMerchantWhiteListMapper.selectOne(wrapper);
        if (selected != null){
            if (!selected.getId().equals(entity.getId())){
                return ApiResp.paramError("修改的商户对应ip已存在");
            }
        }
        sysMerchantWhiteListMapper.updateById(entity);
        return ApiResp.sucess();
    }
}




